cmake_minimum_required(VERSION 2.8)
project(mqttclient_emqx)

##################
## set arg info ##
##################
set(CMAKE_C_FLAGS "-Wall")
set(CMAKE_C_FLAGS "-lpthread")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS} -O0 -g -no-pie -ggdb")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS} -O3 -DNDEBUG")

# set(CMAKE_CXX_STANDARD "-std=c++11")
# set(CMAKE_CXX_STANDARD_REQUIRED True)

# if(CMAKE_COMPILER_IS_GNUCXX)
set(CMAKE_CXX_FLAGS "-std=c++11")
set(CMAKE_CXX_FLAGS "-lpthread")
set(CMAKE_CXX_FLAGS "-Wall")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -O0 -g -no-pie -ggdb")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} -O3")
# endif(CMAKE_COMPILER_IS_GNUCXX)


add_compile_options(-std=c++11)
message(STATUS "optional:-std=c++11")

#########################
## find depend package ##
#########################
# if(BUILD_EXAMPLES)
#     set(PACKAGE_DEPEND ${MODULE_NAME})
# endif()

set(PACKAGE_DEPEND ${MODULE_NAME})
message(STATUS "MODULE_NAME: =========" ${MODULE_NAME})

find_package("Threads")

###########
## build ##
###########

# 指定头文件搜索目录
set(INC_DIRS
    ${CMAKE_CURRENT_SOURCE_DIR}
    ${CMAKE_CURRENT_SOURCE_DIR}/include
    ../common/spdlog
    ../common/rapidjson
    ../common/cpp-httplib
)


# 指定依赖文件搜索目录
set(DEPEND_SRC_DIRS
    logging.cpp
)

# 指定应用文件搜索目录
set(EXAMPLE_SRC_DIRS
    ${CMAKE_CURRENT_SOURCE_DIR}
)

# foreach(FOREACH_SRC_DIR ${DEPEND_SRC_DIRS})
#     aux_source_directory(${FOREACH_SRC_DIR} DEPEND_SOURCES)
# endforeach()

#aux_source_directory 查找在某个路径下的所有源文件
#aux_source_directory(< dir > < variable >)
#add_executable(main ${SRC_LIST})

# foreach(FOREACH_SRC_DIR ${EXAMPLE_SRC_DIRS})
#     aux_source_directory(${FOREACH_SRC_DIR} EXAMPLE_SOURCES)
#     message(STATUS "build example : ===================",${EXAMPLE_SOURCES})
# endforeach()

# 获取父文件夹
get_filename_component(SECOND_FOLDER_ABSOLUTE ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY)
get_filename_component(SECOND_FOLDER_F ${SECOND_FOLDER_ABSOLUTE} DIRECTORY)
get_filename_component(SECOND_FOLDER ${SECOND_FOLDER_ABSOLUTE} NAME)
#设置libevent 路径
include_directories(
    ${SECOND_FOLDER_F}/libevent
    ${SECOND_FOLDER_F}/libevent/include
    ${SECOND_FOLDER_F}/libevent/include/event2
)
# message(STATUS "========SECOND_FOLDER_ABSOLUTE===============" ${SECOND_FOLDER_ABSOLUTE})
# message(STATUS "========SECOND_FOLDER========= " ${SECOND_FOLDER})
# message(STATUS "========SECOND_FOLDER_F========= " ${SECOND_FOLDER_F})
include_directories(${INC_DIRS})

aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR} DEPEND_SOURCES)

#  编译执行
add_executable(${PROJECT_NAME} ${DEPEND_SOURCES})
#链接依赖
target_link_libraries(${PROJECT_NAME} ${CMAKE_THREAD_LIBS_INIT} ${PACKAGE_DEPEND})

#libevent 依赖链接
target_link_libraries(${PROJECT_NAME}
                ${CMAKE_BINARY_DIR}/libevent/lib/libevent.a
                ${CMAKE_BINARY_DIR}/libevent/lib/libevent_core.a
                ${CMAKE_BINARY_DIR}/libevent/lib/libevent_extra.a
                ${CMAKE_BINARY_DIR}/libevent/lib/libevent_pthreads.a
                ${CMAKE_BINARY_DIR}/libevent/lib/libevent.so
                ${CMAKE_BINARY_DIR}/libevent/lib/libevent_extra.so
                ${CMAKE_BINARY_DIR}/libevent/lib/libevent_pthreads.so
)


message(STATUS "build example : =========" ${PROJECT_NAME})
message(STATUS "CMAKE_THREAD_LIBS_INIT: =========" ${CMAKE_THREAD_LIBS_INIT})
message(STATUS "PACKAGE_DEPEND: =========" ${PACKAGE_DEPEND})
message(STATUS "CMAKE_BINARY_DIR: =========" ${CMAKE_BINARY_DIR})



#get_filename_component提取文件名成分
# foreach(example_src_name ${EXAMPLE_SOURCES})
#     get_filename_component(example_name ${example_src_name} NAME_WE)
#     add_executable(${example_name}_example ${example_src_name} ${DEPEND_SOURCES})
#     target_link_libraries(${example_name}_example ${CMAKE_THREAD_LIBS_INIT} ${PACKAGE_DEPEND})
#     message(STATUS "build example : ${example_name} ")
# endforeach()


